<?php

class Default_UserController extends Zend_Controller_Action
{
    
    public function init()
    {
        /* Initialize action controller here */
        $this->view->headTitle("User")->setSeparator(" | ");
    }

    public function indexAction()
    {
        // action body
    }
    
    public function logoutAction()
    {
        $auth = Zend_Auth::getInstance();
        if($auth->hasIdentity())
        {
            $auth->clearIdentity();
        }
        Zend_Session::destroy();
        return $this->_redirect($this->view->baseUrl("/"));
    } 
    
    public function loginAction()
    {
        if($this->_request->isPost())
        {
            $username = $this->_request->getParam("user","");
            $password = $this->_request->getParam("pass","");
            if($username!="")            
            {
                
                $auth = Zend_Auth::getInstance();
                $autAdapter = new Zend_Auth_Adapter_DbTable(null, "customers", "username", "password");            
                $autAdapter->setIdentity($username)
                           ->setCredential(md5($password));
                $select = $autAdapter->getDbSelect();
                $select->where("status=?",1);                
                $result = $auth->authenticate($autAdapter);                
                if($result->isValid())
                {
                    $store = $auth->getStorage();
                    $store->write($autAdapter->getResultRowObject(array(
                        "id","username","fullname","email","address","phone","intro"
                    )));
                    $this->_helper->json->sendJson(true);
                }   
                else
                {
                    $this->_helper->json->sendJson(false);
                }
            }
            else
            {
                $this->_helper->json->sendJson(false);
            }
        }
        $this->_helper->layout()->disableLayout();
        $this->_response->clearBody();
    }
    
    public function registryAction()
    {
        $this->view->headTitle("Registry")->setSeparator(" | ");
        
        $form = new Application_Form_Registry();
        
        if($this->_request->isPost())
        {   
            if($form->isValid($_POST))
            {
                $data = $form->getValues();
                if($data['password']!=$data['repassword'])
                {
                    $this->view->er_string = "Password does not match the confirm password.";
                    $this->view->er_key = "repassword";
                }
                else
                {
                    $mdCustomer = new Application_Model_DbTable_Customer();
                    $customer = $mdCustomer->fetchRow($mdCustomer->select()->where("username=?",$data['username']));
                    if(!$customer)
                    {
                        unset($data['repassword']);
                        unset($data['captcha']);
                        $row = $mdCustomer->createRow($data);
                        
                        $row->password = md5($row->password);
                        $row->regestry_date = date("Y-m-d H:i:s");
                        $row->intro = serialize(array("visacard"=>$data['visa']));
                        $row->status = 1;
                        $row->save();
                        $this->view->er_string = "";
                    }
                    else
                    {
                        $this->view->er_string = "Username already exists!";
                        $this->view->er_key = "username";
                    }
                }            
               
            }
        }
        $this->view->form = $form;
        
    }
    
    
    

}

